package ca.camosun.inventory.gui;

import net.sourceforge.zbar.Symbol;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import ca.camosun.inventory.Box;
import ca.camosun.inventory.R;
import ca.camosun.inventory.database.BoxAccessor;
import ca.camosun.inventory.database.BoxAccessor.BoxList;

import com.dm.zbar.android.scanner.ZBarConstants;
import com.dm.zbar.android.scanner.ZBarScannerActivity;

public class AddBoxActivity extends Activity {

	String nameText;
	EditText name;
	EditText qrLocation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_box);
		
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);

		name = (EditText) findViewById(R.id.editTextName);
		qrLocation = (EditText) findViewById(R.id.uniqueCode);

	}
	
	@Override 
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.home_root) {
			Intent intent = new Intent(this, MainActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		} else if (item.getItemId() == android.R.id.home) {
			Intent upIntent = new Intent (this, MainActivity.class);
			upIntent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(upIntent);
			return true;
		}	
		return true;
	}
	
	public void getQR(View view) {
		nameText = name.getText().toString();
		Intent intent = new Intent(this, ZBarScannerActivity.class);
		intent.putExtra(ZBarConstants.SCAN_MODES, new int[] { Symbol.QRCODE });
		startActivityForResult(intent, ZBarConstants.ZBAR_QR_SCANNER_REQUEST);
	}

	public void createBox(View view) {

		if(name.getText().toString().isEmpty()) {
			Toast.makeText(
					this,
					"Please enter a box name",
					Toast.LENGTH_SHORT).show();
			return;
		}
		
		if(qrLocation.getText().toString().isEmpty()) {
			Toast.makeText(
					this,
					"Please enter a QR code",
					Toast.LENGTH_SHORT).show();
			return;
		}
		
		String boxName = name.getText().toString();
		String qrLocationCode = qrLocation.getText().toString();

		Box newBox = new Box(boxName, qrLocationCode, this);

		BoxAccessor boxDatabase = new BoxAccessor(this);
		boxDatabase.open();
		BoxList status = boxDatabase.addBox(newBox.getName(), newBox.getCode());
		boxDatabase.close();
		
		if (status != BoxList.BOX_ADDED ) {
			Toast.makeText(
					this,
					status.toString(),
					Toast.LENGTH_SHORT).show();
			return;
		}

		finish();

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			qrLocation.setText(data.getStringExtra(ZBarConstants.SCAN_RESULT));
			Toast.makeText(
					this,
					R.string.success_scan,
					Toast.LENGTH_SHORT).show();
		}
		name.setText(nameText);
	}
	
}


